package com.pig4cloud.pig.homecenter.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.security.service.PigUser;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.homecenter.entity.HassDeviceRegistryEntity;
import com.pig4cloud.pig.homecenter.entity.HassEntityRegistryEntity;
import com.pig4cloud.pig.homecenter.mapper.HassDeviceRegistryMapper;
import com.pig4cloud.pig.homecenter.mapper.HassEntityRegistryMapper;
import com.pig4cloud.pig.homecenter.service.HassEntityRegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实体数据
 *
 * @author pig
 * @date 2025-01-09 13:43:05
 */
@Service
public class HassEntityRegistryServiceImpl extends ServiceImpl<HassEntityRegistryMapper, HassEntityRegistryEntity> implements HassEntityRegistryService {

	private final EndpointMediaTypes endpointMediaTypes;

	@Autowired
	HassDeviceRegistryMapper hassDeviceRegistryMapper;

	public HassEntityRegistryServiceImpl(EndpointMediaTypes endpointMediaTypes) {
		this.endpointMediaTypes = endpointMediaTypes;
	}

	@Override
	public void syncHassEntityRegistry(List<HassEntityRegistryEntity> entityList) {
		PigUser user = SecurityUtils.getUser();
		entityList.forEach(entity -> {
			if(entity.getOriginalName().length() > 64){
				return;
			}
			entity.setUserId(user.getId());

			QueryWrapper<HassEntityRegistryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(HassEntityRegistryEntity::getUserId, entity.getUserId())
					.eq(HassEntityRegistryEntity::getId, entity.getId());
			HassEntityRegistryEntity curEntity = getOne(queryWrapper, false);
			if (curEntity != null) {
				curEntity.setName(entity.getName());
				curEntity.setOriginalName(entity.getOriginalName());
				curEntity.setModifiedAt(entity.getModifiedAt());
				curEntity.setConfigEntryId(entity.getConfigEntryId());
				curEntity.setEntityId(entity.getEntityId());
				curEntity.setPlatform(entity.getPlatform());
				curEntity.setUniqueId(entity.getUniqueId());
				curEntity.setDeviceId(entity.getDeviceId());
//				if(!StringUtils.isBlank(entity.getAreaId())){
//					entity.setAreaId(curEntity.getAreaId());
//				}
//				curEntity.setAreaId(entity.getAreaId());
				curEntity.setIcon(entity.getIcon());
				curEntity.setDisabledBy(entity.getDisabledBy());
				curEntity.setTranslationKey(entity.getTranslationKey());
				curEntity.setHasEntityName(entity.getHasEntityName());
				curEntity.setHiddenBy(entity.getHiddenBy());
				updateById(curEntity);
			}else {
				QueryWrapper<HassDeviceRegistryEntity> deviceWrapper = new QueryWrapper<>();
				deviceWrapper.lambda().eq(HassDeviceRegistryEntity::getId, entity.getDeviceId());
				HassDeviceRegistryEntity device = hassDeviceRegistryMapper.selectOne(deviceWrapper,false);
				if(device != null && StringUtils.isBlank(entity.getAreaId())){
					entity.setAreaId(device.getAreaId());
				}
				if (!StringUtils.isBlank(entity.getName())){
					entity.setCustomeName(entity.getName());
				}else{
					entity.setCustomeName(entity.getOriginalName());
				}
				String type = entity.getEntityId().split("\\.")[0];
				entity.setShowAs(type);
				entity.setEntityType(type);
				entity.setVisible(true);
				save(entity);
			}
		});
	}
}
